//
//  BKMainCommon.m
//  bestkeep
//
//  Created by 毛韶谦 on 2016/11/3.
//  Copyright © 2016年 utouu. All rights reserved.
//

#import "BKMainCommon.h"


NSString *const BKMainLocalizableTableName = @"BKMainLocalizable";



@implementation BKMainCommon

+(NSString *)localizedStringWithKey:(NSString *)key {
    return NSLocalizedStringFromTable(key, BKMainLocalizableTableName, nil);
}
+(BOOL)checkNetWork{
    NSInteger numbe = [RequestManager reachabilityStatus];
    NSLog(@"网络状态-----%ld",(long)numbe);
    if ([RequestManager reachabilityStatus] == AFNetworkReachabilityStatusReachableViaWiFi ) {
        return YES;
    }else if ([RequestManager reachabilityStatus] == RequestReachabilityStatusReachableViaWWAN){
        return YES;
    }else if ([RequestManager reachabilityStatus] == RequestReachabilityStatusNotReachable){
        return NO;
    }else{
        return NO;
    }
    
}
+(NSString *)getUserId{
    if ([self islogin]) {
        Mediator *mediator = [Mediator sharedInstance];
        NSDictionary *dic = [mediator BKMyUserInfo];
        NSString *result = dic[BKMyUserInfoGetUserIdKey];
        return result;
    }
    return nil;
}

+(NSString *)getUserST{
    if ([self islogin]) {
        Mediator *mediator = [Mediator sharedInstance];
        NSDictionary *dic = [mediator BKMyUserInfo];
        NSString *result = dic[BKMyUserGetUTOUUPlatformSTkey];
        return result;
    }
    return nil;
}
+(BOOL)islogin{
    Mediator *mediator = [Mediator sharedInstance];
    NSDictionary *dic = [mediator BKMyUserInfo];
    NSNumber *result = dic[BKMyUserIsLoginKey];
    if ([result intValue] == 0) {
        return NO;
    }
    return YES;

}
+ (NSString *)ServicesURL:(NSString *)url
{
    NSString *servicesURL = ApplicationEnvironmentProduction ? @"http://api.bestkeep.cn" : @"http://api.test.bestkeep.cn";
    NSString *URLString = [NSString stringWithFormat:@"%@/%@", servicesURL, url];
    return URLString;
}


+(NSMutableAttributedString *)specialStringStyle:(NSString*)string fontOfSize:(CGFloat)fontOfSize originalPrice:(NSString*)originalPrice{
    
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc]initWithString:string];
    NSArray *array = [string componentsSeparatedByString:@" "];
    if ([[array firstObject] rangeOfString:@"起"].location != NSNotFound) {
        NSRange range = [string rangeOfString:@"起"];
        [str addAttribute:NSForegroundColorAttributeName value:RGBACOLOR(178, 178, 178, 1) range:NSMakeRange(range.location, string.length - range.location)];
        [str addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:fontOfSize] range:NSMakeRange(range.location, string.length - range.location)];
        [str addAttribute:NSStrikethroughStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:NSMakeRange(range.location + 2, string.length - range.location - 2)];
        return str;
    }else{
        NSRange range = [string rangeOfString:@" "];
        [str addAttribute:NSForegroundColorAttributeName value:RGBACOLOR(178, 178, 178, 1) range:NSMakeRange(range.location, string.length - range.location)];
        [str addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:fontOfSize] range:NSMakeRange(range.location, string.length - range.location)];
        [str addAttribute:NSStrikethroughStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:NSMakeRange(range.location + 1, string.length - range.location - 1)];
        return str;
    }
    
}
+(NSMutableAttributedString *)specialStringStyle:(NSString*)string fontOfSize:(CGFloat)fontOfSize{
    
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc]initWithString:string];
    if ([string rangeOfString:@"起"].location != NSNotFound) {
        NSRange range = [string rangeOfString:@"起"];
        [str addAttribute:NSForegroundColorAttributeName value:RGB(136, 136, 136) range:NSMakeRange(range.location, string.length - range.location)];
        [str addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:fontOfSize] range:NSMakeRange(range.location, string.length - range.location)];
    }
    return str;
}

+(CGSize)sizeWithContentString:(NSString *)content textFont:(UIFont *)font size:(CGSize)contentSize
{
    NSDictionary * fontDict = [NSDictionary dictionaryWithObjectsAndKeys:font,NSFontAttributeName,nil];
    CGRect rect = [content boundingRectWithSize:contentSize options:NSStringDrawingUsesLineFragmentOrigin attributes:fontDict context:nil];
    
    return rect.size;
}

//设置首行缩进
+(void)setFirstLineTextIndentWithContent:(UILabel*)contentLabel firstLineHeadIndent:(CGFloat)value
{
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:contentLabel.text];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:3];
    paragraphStyle.firstLineHeadIndent= value;
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [contentLabel.text length])];
    contentLabel.attributedText = attributedString;
    contentLabel.lineBreakMode = NSLineBreakByTruncatingTail;
}

+(void)getNewFirstPageData:(CompeletionCallBack)callback{
    
    RequestManager *manger = [RequestManager manager];
    [manger bkDataRequest:RequestMethodGet URLString:[self ServicesURL:@"index_v228"] parameters:nil success:^(id  _Nullable responseObject) {
        NSLog(@"%@",responseObject);
        if ([responseObject isKindOfClass:[NSDictionary class]]) {
            BOOL success = [[responseObject objectForKey:@"success"] boolValue];
            if (success) {
                NSDictionary *data_dic = [responseObject objectForKey:@"data"];
                callback(data_dic,nil);
            }

        }
        
    } failure:^(id  _Nullable responseObject, NSError * _Nullable error) {
            callback(nil,error);
        NSLog(@"%@",error);
    }];
}


+(void)getFeaturedData:(NSString *)page size:(NSString *)size callback:(CompeletionCallBack)callback{
    NSDictionary *parametersDic = [NSDictionary dictionaryWithObjectsAndKeys:page,@"page", size, @"size", nil];
   
    
    RequestManager *manger = [RequestManager manager];
    [manger bkDataRequest:RequestMethodPost URLString: [self ServicesURL:@"bkjxPageList"] parameters:parametersDic success:^(id  _Nullable responseObject) {
        
        NSDictionary *result_dic = responseObject;
        if ([responseObject isKindOfClass:[NSDictionary class]]) {
            if ([[responseObject allKeys] containsObject:@"success"]) {
                NSDictionary *data_dic = [result_dic objectForKey:@"data"];
                callback(data_dic,nil);
            }
        }
        
    } failure:^(id  _Nullable responseObject, NSError * _Nullable error) {
                callback(nil,error);
        
    }];
}

+(void)getBKPhoneCardsBannerData:(CompeletionCallBack)callBack{
    
    RequestManager *manager = [RequestManager manager];
    [manager bkDataRequest:RequestMethodPost URLString:[self ServicesURL:@"mbop/getBannerInfo"] parameters:nil success:^(id  _Nullable responseObject) {
        NSLog(@"%@",responseObject);
        NSDictionary *result_dic = responseObject;
        if ([result_dic[@"success"] boolValue]) {
            NSDictionary *dataDic = result_dic[@"data"];
            callBack(dataDic,nil);
        }
    } failure:^(id  _Nullable responseObject, NSError * _Nullable error) {
        callBack(nil,error);
    }];
}

+(void)getPhoneSelectInfo:(CompeletionCallBack)callback account_no:(NSString *)account_no{
    NSDictionary *paramDic = [NSDictionary dictionaryWithObject:account_no forKey:@"account_no"];
    RequestManager *manager = [RequestManager manager];
    
    [manager bkDataRequest:RequestMethodPost URLString:[self ServicesURL:@"mbop/getMobileShelfInfos"] parameters:paramDic success:^(id  _Nullable responseObject) {
        NSDictionary *dic = responseObject;
        if ([dic[@"success"] boolValue]) {
            if (!dic[@"data"]) {
                callback(nil,nil);
            }else {
                NSDictionary *result_dic = dic[@"data"];
                BKPhoneCardsList *phoneCardsList = [[BKPhoneCardsList alloc] init];
                phoneCardsList.accountNo = [NSString stringWithFormat:@"%@",[result_dic objectForKey:@"accountNo"]];
                phoneCardsList.cityName = [NSString stringWithFormat:@"%@",[result_dic objectForKey:@"cityName"]];
                phoneCardsList.ispId = [NSString stringWithFormat:@"%@",[result_dic objectForKey:@"ispId"]];
                phoneCardsList.ispIdName = [NSString stringWithFormat:@"%@",[result_dic objectForKey:@"ispIdName"]];
                phoneCardsList.provinceName = [NSString stringWithFormat:@"%@",[result_dic objectForKey:@"provinceName"]];
                NSMutableArray *arrayList = [[result_dic objectForKey:@"shelfInfos"] mutableCopy];
                
                phoneCardsList.shelfInfos = [NSMutableArray array];
                if ( arrayList == nil || arrayList.count == 0) {
                    
                    callback(nil,nil);
                }else {
                    
                    for (NSDictionary *dic in arrayList) {
                        ShelfInfos *shelfInfos = [[ShelfInfos alloc] init];
                        shelfInfos.facePrice = [NSString stringWithFormat:@"%@",[dic objectForKey:@"facePrice"]];
                        shelfInfos.salePrice = [NSString stringWithFormat:@"%@",[dic objectForKey:@"salePrice"]];
                        shelfInfos.isHasPreferential = [NSString stringWithFormat:@"%@",[dic objectForKey:@"firstRechargePrivilegeFlag"]];
                        [phoneCardsList.shelfInfos addObject:shelfInfos];
                    }
                    callback(phoneCardsList,nil);
                }
            }
            
        } else {
            callback(nil, nil);
            if ([[dic valueForKey:@"code"] isEqualToString:@"020"]) {
                NSString *msg = [dic valueForKey:@"msg"];
                [HUD showMessage:msg];
            }
        }
    } failure:^(id  _Nullable responseObject, NSError * _Nullable error) {
        callback(nil, error);
    }];
}

+(void)getPhoneFlowInfo:(CompeletionCallBack)callback account_no:(NSString *)account_no{
    
    
    NSDictionary *paramDic = [NSDictionary dictionaryWithObject:account_no forKey:@"account_no"];
    RequestManager *manager = [RequestManager manager];
    [manager bkDataRequest:RequestMethodPost URLString: [self ServicesURL:@"mbop/getTraficShelfInfos"] parameters:paramDic success:^(id  _Nullable responseObject) {
        
        NSDictionary *dic = responseObject;
        if ([dic[@"success"] boolValue]) {
            if (!dic[@"data"]) {
                callback(nil,nil);
            }else {
                
                NSDictionary *result_dic = dic[@"data"];
                BKPhoneCardsList *phoneCardsList = [[BKPhoneCardsList alloc] init];
                phoneCardsList.accountNo = [NSString stringWithFormat:@"%@",[result_dic objectForKey:@"accountNo"]];
                phoneCardsList.cityName = [NSString stringWithFormat:@"%@",[result_dic objectForKey:@"cityName"]];
                phoneCardsList.ispId = [NSString stringWithFormat:@"%@",[result_dic objectForKey:@"ispId"]];
                phoneCardsList.ispIdName = [NSString stringWithFormat:@"%@",[result_dic objectForKey:@"ispIdName"]];
                phoneCardsList.provinceName = [NSString stringWithFormat:@"%@",[result_dic objectForKey:@"provinceName"]];
                NSMutableArray *arrayList = [[result_dic objectForKey:@"shelfInfos"] mutableCopy];
                
                phoneCardsList.shelfInfos = [NSMutableArray array];
                if (arrayList == nil || arrayList.count == 0) {
                    callback(nil,nil);
                }else {
                    
                    for (NSDictionary *dic in arrayList) {
                        ShelfInfos *shelfInfos = [[ShelfInfos alloc] init];
                        shelfInfos.facePrice = [NSString stringWithFormat:@"%@",[dic objectForKey:@"facePrice"]];
                        shelfInfos.salePrice = [NSString stringWithFormat:@"%@",[dic objectForKey:@"salePrice"]];
                        shelfInfos.isHasPreferential = [NSString stringWithFormat:@"%@",[dic objectForKey:@"firstRechargePrivilegeFlag"]];
                        [phoneCardsList.shelfInfos addObject:shelfInfos];
                    }
                    callback(phoneCardsList,nil);
                }
            }
        }else {
            callback(nil, nil);
            if ([[dic valueForKey:@"code"] isEqualToString:@"020"]) {
                NSString *msg = [dic valueForKey:@"msg"];
                [HUD showMessage:msg];
            }
        }
        
    } failure:^(id  _Nullable responseObject, NSError * _Nullable error) {
        callback(nil, error);
    }];
    
    
}

//获取手机充值记录
+(void)getPhoneCardsRecord:(NSString *)currentPage myCallback:(CompeletionCallBack)callback{
    NSDictionary *paramDic = [NSDictionary dictionaryWithObject:currentPage forKey:@"currentPage"];
    RequestManager *manager = [RequestManager manager];
    [manager bkDataRequest:RequestMethodPost URLString:[self ServicesURL:@"mbop/getOrderList"] parameters:paramDic success:^(id  _Nullable responseObject) {
        NSDictionary *dic = responseObject;
        if ([dic[@"success"] boolValue]) {
            NSDictionary *dataDic = dic[@"data"];
            callback(dataDic,nil);
        }

    } failure:^(id  _Nullable responseObject, NSError * _Nullable error) {
        callback(nil,error);
    }];
}

//话费充值订单生成
+ (void)phonePayOrder:(NSMutableDictionary *)body_dic callback:(CompeletionCallBack)callback {
    NSString *url = [BKMainCommon ServicesURL:@"mbop/submitOrder"];
    [[RequestManager manager] bkDataRequest:RequestMethodPost URLString:url parameters:body_dic success:^(id  _Nullable responseObject) {
        NSDictionary *result_dict = responseObject;
        NSDictionary *dict = result_dict[@"data"];
        if ([result_dict[@"success"] boolValue]) {
            callback(dict , nil);
        }else {
            callback(nil, nil);
        }
    } failure:^(id  _Nullable responseObject, NSError * _Nullable error) {
        callback(nil, error);
    }];
}

+ (void)getPhonePayOrderAlipayInfoWithOrderNo:(NSString *)orderNo callback:(CompeletionCallBack)callback {
    NSString *url = [BKMainCommon ServicesURL:@"mbop/getOrderPayInfo"];
    NSMutableDictionary *parameterDict = [NSMutableDictionary dictionary];
    [parameterDict setObject:orderNo forKey:@"order_no"];
    [[RequestManager manager] bkDataRequest:RequestMethodPost URLString:url parameters:parameterDict success:^(id  _Nullable responseObject) {
        NSDictionary *dic = responseObject;
        if ([dic[@"success"] boolValue]) {
            OrderAlipayInfo *orderAlipayInfo = [[OrderAlipayInfo alloc] init];
            [orderAlipayInfo setValuesForKeysWithDictionary:dic[@"data"]];
            callback(orderAlipayInfo, nil);
        }else {
            callback(nil, nil);
        }
    } failure:^(id  _Nullable responseObject, NSError * _Nullable error) {
        callback(nil, error);
    }];
}
+(void)getAPPStoreEvaluateCallBack:(CompeletionCallBack)callback{
    NSString *url = [BKMainCommon ServicesURL:@"app-store-evaluate"];
    NSMutableDictionary *parameterDict = [NSMutableDictionary dictionary];
    [parameterDict setObject:[NSString stringWithFormat:@"%@",APP_VERSION] forKey:@"version"];
    [[RequestManager manager] bkDataRequest:RequestMethodPost URLString:url parameters:parameterDict success:^(id  _Nullable responseObject) {
        NSDictionary *dic = responseObject;
        if ([dic[@"success"] boolValue]) {
            
            callback(dic[@"data"],nil);// "flag": true,
        }
    } failure:^(id  _Nullable responseObject, NSError * _Nullable error) {
            callback(nil,error);
    }];
}

@end
